package com.chenjian.codingstylesecurity.service.impl;

import com.chenjian.codingstylesecurity.dao.UserDao;
import com.chenjian.codingstylesecurity.entity.MyUser;
import com.chenjian.codingstylesecurity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019/1/8 0:04
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<MyUser> users = userDao.findByUsername(username);
        if (null != users && users.size() == 1) {
            MyUser user = users.get(0);

            List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");

            User user1 = new User(user.getUsername(), user.getPassword(), true, true, true, true, authorities);
            return user1;
        }

        return null;
    }
}
